<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>学生课表</title>
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>
    <script>
        $(document).ready(function(){
            var user = window.localStorage.getItem("username");
            var data = $.parseJSON(user);
            console.log(data);
            if(data.user_type !== 3){
                alert("您不是学生，无法访问此页面");
                window.location.href = "user_courses.html";
            }

            $("#scheduleForm").submit(function(event){
                event.preventDefault();
                var formData = $(this).serialize();
                formData += "&student_user_id=" + data.student_id;
                $.post("http://localhost:80/edu/student/schedule", formData, function(response) {
                    var rs = $.parseJSON(response);
                    if (rs.code != "00000" || !rs.data) {
                        alert("获取课表数据失败");
                        return;
                    }
                    displaySchedule(rs.data);
                });
            });
        });

        function displaySchedule(schedule) {
            var table = $("#scheduleTable");
            table.empty();
            table.append("<tr><th>课程名称</th><th>课程教室</th><th>上课时间</th><th>课程老师</th></tr>");
            $.each(schedule, function(index, course) {
                var row = $("<tr></tr>");
                row.append($("<td></td>").text(course.course_name));
                row.append($("<td></td>").text(course.course_room));
                row.append($("<td></td>").text(course.course_time));
                row.append($("<td></td>").text(course.teacher_realname));
                table.append(row); // 确保将行添加到表格中
            });
        }
    </script>
    <style>
        .container {
            text-align: center;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>学生课表</h1>
    <a href="user_courses.html">我的课程</a><br>
    <a href="all_courses.html">选课</a><br>
    <a href="all_blogs.html">校园论坛</a><br>
    <a href="change_pwd.html">修改密码</a><br>
    <a href="teacher_schedule.html">教师课表</a><br>
    <form id="scheduleForm">
        学年：<input type="text" name="course_year"><br>
        学期：<input type="text" name="course_semester"><br>
        <input type="submit" value="查看课表">
    </form>
    <table id="scheduleTable" border="1">
        <thead>
        <tr>
            <th>课程名称</th>
            <th>课程教室</th>
            <th>课程时间</th>
            <th>课程老师</th>
        </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
</div>
</body>
</html>